#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct 
{
    char name[11];  
    double total_score;  
} Contestant;
void swap(Contestant *a, Contestant *b) 
{
    Contestant temp = *a;
    *a = *b;
    *b = temp;
}
void bubbleSort(Contestant arr[], int n) 
{
    for (int i = 0; i < n - 1; i++) 
	{
        for (int j = 0; j < n - i - 1; j++) 
		{
            if (arr[j].total_score < arr[j + 1].total_score) 
			{
                swap(&arr[j], &arr[j + 1]);
            } 
            else if (arr[j].total_score == arr[j + 1].total_score && strcmp(arr[j].name, arr[j + 1].name) > 0) 
			{
                swap(&arr[j], &arr[j + 1]);
            }
        }
    }
}

int main() 
{
    int n, m;
    scanf("%d %d", &n, &m);
    int *weights = (int *)malloc(m * sizeof(int));
    for (int i = 0; i < m; i++) 
	{
        scanf("%d", &weights[i]);
    }
    Contestant *contestants = (Contestant *)malloc(n * sizeof(Contestant));
    for (int i = 0; i < n; i++) 
	{
        scanf("%s", contestants[i].name);
        contestants[i].total_score = 0.0;
        for (int j = 0; j < m; j++) 
		{
            int score;
            scanf("%d", &score);
            contestants[i].total_score += score * weights[j];
        }
    }
    bubbleSort(contestants, n); 
    for (int i = 0; i < n; i++) 
	{
        printf("%s\n", contestants[i].name);
    }
    free(weights);
    free(contestants); 
    return 0;
}
